<template>
	<view class="teacherdetail">
		<view class="title">
			<u-icon name="arrow-left" size="24" @click="back"></u-icon>
			<text style="margin-left: 24rpx;">导师详情</text>
		</view>
		<view class="detailbox">
			<view class="teacherimgbox">
				<image class="teacherimg" :src="teacherDetail.photos.url"></image>
			</view>
			<view class="teachermessage">
				<view class="messagedetail">
					<view class="messagedetailleft">
						<view class="name">
							{{teacherDetail.name}}
						</view>
						<view class="jibie">
							{{teacherDetail.category_text}}
						</view>
						<view class="zhuanye">
							<view class="zhuanyeitem" v-for="(item,index) in teacherDetail.science_ids">
								{{item}}
							</view>
						</view>
					</view>
					<view class="messagedetailright">
						<view v-if="teacherDetail.is_student== 0" class="selectbtn" @click="handleselect">
							选择该导师
						</view>
						<view v-if="teacherDetail.is_student== 1" class="selectbtn" style="background-color: #FE532D;">
							已有导师
						</view>
						<view v-if="teacherDetail.is_student== 2" class="selectbtn" style="background-color: #FE532D;">
							待确认
						</view>
						<view v-if="teacherDetail.is_student== 3" class="selectbtn" style="background-color: #FE532D;">
							导师学生已满
						</view>
					</view>

				</view>
				<view class="jianjie">
					<view class="jianjietitle">
						导师简介
					</view>
					<view class="jianjiecontent">
						{{teacherDetail.brief_introduction}}
					</view>

				</view>

				<view class="fujian">
					<view class="fujiantitle">
						附件
					</view>
					<view class="fujianbox">
						<view class="fujianitem" v-for="(item,index) in fujiandata" @click="handlexiazai(item)">
							<view class="imgbox">
								<image class="" style="width: 100%; height: 100%;" src="../../static/image/files.png"></image>
							</view>
							<view class="fileName">
								{{item.name}}
							</view>
							<view class="imgbox">
								<image class="" style="width: 100%; height: 100%;" src="../../static/image/download.png"
									></image>
							</view>
						</view>
					</view>
				</view>


			</view>
		</view>

		
	</view>
</template>

<script>
	import {
		getteacherDetail,
		selectteacher
	} from "@/api/api.js"
	export default {
		data() {
			return {
				show: false,
				teacherDetail: {},
				fujiandata: [],
			}
		},
		onLoad: function(option) {
			console.log("option.id", option.id)
			this.getteacherDetails(option.id)
		},
		methods: {
			handleselect() {
				let that = this
				uni.showModal({
					// title: '提示',
					content: '确定要选择该导师吗？学生只能绑定1位导师，导师确认后如需解绑请联系客服',
					confirmColor: '#4A9CFF',
					success: async function(res) {
						console.log("是否选择", res)
						if (res.confirm) {
							that.subok()
						}
					}
				})
				// this.show = true
			},
			close() {
				this.show = false
			},
			async subok() {
				this.show = false
				let res = await selectteacher({
					teacher_id: this.teacherDetail.id
				})
				if (res.code == 1) {
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 1000
					});
					this.getteacherDetails(this.teacherDetail.id)

				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 1000
					});
				}

			},
			async getteacherDetails(id) {
				let res = await getteacherDetail({
					teacher_id: id
				})
				if (res.code == 1) {
					console.log("导师详情", res)
					this.teacherDetail = res.data
					this.teacherDetail.science_ids = this.teacherDetail.science_ids_text.split(",")
					this.fujiandata = res.data.annex
				}

			},
			handlexiazai(item) {
				let that = this
				uni.showModal({
					// title: '提示',
					content: '确定下载并查看附件吗？',
					confirmColor: '#4A9CFF',
					success: async function(res) {
						console.log("是否选择", res)
						if (res.confirm) {
							that.xiazaiFujian(item)
						}
					}
				})
			},
			xiazaiFujian(item) {
				let that = this
				console.log(item.url);
				uni.downloadFile({
					url: item.url, //仅为示例，并非真实的资源
					success: (res) => {
						console.log(res,'222222');
						// uni.showToast({
						// 	title: '55555555555555！',
						// 	icon: 'none',
						// 	duration: 1000
						// })
						
						// return;
						if (res.statusCode == 200) {
							console.log('下载成功', res);
							let fileType = that.getfileName(res.tempFilePath)
							uni.openDocument({
								// 文件路径
								filePath: res.tempFilePath,
								// 文件类型 除图片以外文件都需要文件类型  
								//有效值为  doc, xls, ppt, pdf, docx, xlsx, pptx
								fileType: fileType,
								//是否开启转发 
								showMenu: true,
							})

						}
					},
					fail: (res) => {
						console.log(res,'1111111');
					}
				});
			},
			getfileName(filename) {
				// 转换为小写方便处理 拿到.的下标
				const pos = filename.toLowerCase().lastIndexOf('.')
				let suffix = ''
				// 判断是否是文件或图片
				if (pos != -1) {
					// 拿到截取.的下标后面所有字符(这样就拿到了文件类型)
					suffix = filename.substring(pos + 1)
				}
				return suffix
			},
			back() {
				uni.navigateBack({
					delta: 1
				});
				// uni.redirectTo({
				// 	url: "/pages_students/home/index"
				// })

			}
		}
	}
</script>

<style scoped lang="scss">
	page {
		height: 100%;
	}
	.popcontent {
		width: 610rpx;
		height: 240rpx;
		background: #FFFFFF;
		border-radius: 24rpx 24rpx 24rpx 24rpx;
		opacity: 1;
		padding-top: 50rpx;
		box-sizing: border-box;
		z-index: 999;

		.text {
			font-size: 30rpx;
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: bold;
			color: #333333;
			line-height: 45rpx;
			padding: 0 40rpx;
			display: inline-block;
		}

		.btnbox {
			width: 100%;
			height: 100rpx;
			border-top: 1px solid gainsboro;
			box-sizing: border-box;
			margin-top: 70rpx;
			display: flex;

			.btn {
				width: 50%;
				text-align: center;
				line-height: 100rpx;
				font-size: 32rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #333333;
				line-height: 38rpx;
				padding-top: 30rpx;
				box-sizing: border-box;
			}

			.btnok {
				border-left: 1px solid gainsboro;
				color: #4A9CFF;
			}
		}

	}

	.teacherdetail {
		min-height: 100%;

		.title {
			width: 100%;
			height: 176rpx;
			padding-top: 110rpx;
			box-sizing: border-box;
			// background-color: red;
			display: flex;
			align-items: center;
			font-size: 32rpx;
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: bold;
			color: #333333;
			line-height: 38rpx;
			padding-left: 34rpx;
			box-sizing: border-box;

		}

		.detailbox {
			position: relative;

			.teacherimgbox {
				width: 750rpx;
				height: 800rpx;

				.teacherimg {
					width: 100%;
					height: 100%;
				}
			}

			.teachermessage {
				width: 750rpx;
				background: #FFFFFF;
				border-radius: 40rpx 40rpx 0rpx 0rpx;
				opacity: 1;
				margin-top: -40rpx;
				position: absolute;
				padding: 30rpx;
				box-sizing: border-box;

				.messagedetail {
					display: flex;
					justify-content: space-between;
					// align-items: center;

					.messagedetailleft {
						width: 70%;

						.name {
							font-size: 32rpx;
							font-family: PingFang SC-Bold, PingFang SC;
							font-weight: bold;
							color: #333333;
							line-height: 38rpx;
						}

						.jibie {
							font-size: 26rpx;
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #666666;
							line-height: 30rpx;
							margin: 25rpx 0;
						}

						.zhuanye {
							display: flex;

							.zhuanyeitem {
								width: 164rpx;
								height: 48rpx;
								background: #F0F7FF;
								border-radius: 12rpx 12rpx 12rpx 12rpx;
								font-size: 24rpx;
								font-family: PingFang SC-Regular, PingFang SC;
								font-weight: 400;
								color: #4A9CFF;
								line-height: 28rpx;
								text-align: center;
								line-height: 48rpx;
								margin-right: 10rpx;
							}
						}
					}

					.messagedetailright {
						.selectbtn {
							width: 200rpx;
							height: 80rpx;
							background: #4A9CFF;
							border-radius: 40rpx 40rpx 40rpx 40rpx;
							font-size: 30rpx;
							font-family: PingFang SC-Bold, PingFang SC;
							font-weight: bold;
							color: #FFFFFF;
							line-height: 35rpx;
							line-height: 80rpx;
							text-align: center;
						}
					}
				}

				.jianjie {
					margin-top: 30rpx;
					width: 100%;
					padding: 30rpx 0;

					.jianjietitle {
						font-size: 32rpx;
						font-family: PingFang SC-Bold, PingFang SC;
						font-weight: bold;
						color: #333333;
						line-height: 38rpx;
					}

					.jianjiecontent {
						margin-top: 30rpx;
						width: 690rpx;
						background: #F7F8FA;
						border-radius: 20rpx 20rpx 20rpx 20rpx;
						font-size: 30rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #666666;
						line-height: 47rpx;
						padding: 15rpx;
						box-sizing: border-box;
					}
				}

				.fujian {
					width: 750rpx;
					// margin-top: 40rpx;

					.fujiantitle {
						font-size: 32rpx;
						font-family: PingFang SC-Bold, PingFang SC;
						font-weight: bold;
						color: #333333;
						line-height: 38rpx;
					}

					.fujianbox {
						margin-top: 30rpx;

						.fujianitem {
							width: 690rpx;
							height: 124rpx;
							display: flex;
							justify-content: space-between;
							align-items: center;
							padding: 30rpx;
							box-sizing: border-box;
							background: #F7F8FA;
							border-radius: 20rpx 20rpx 20rpx 20rpx;
							margin-bottom: 20rpx;

							.fileName {
								width: 500rpx;
								font-size: 32rpx;
								font-family: PingFang SC-Regular, PingFang SC;
								font-weight: 400;
								color: #333333;
								line-height: 60rpx;
								white-space: nowrap;
								overflow: hidden;
								text-overflow: ellipsis;
							}

							.imgbox {
								width: 48rpx;
								height: 48rpx;
							}
						}

					}
				}
			}
		}
	}
</style>